package com.lizonghui.progressbutton;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by Administrator on 2016/6/27.
 * 自定义进度view，是的view可以随着下载进度而变化
 */
public class ProgressView extends LinearLayout {

    private ImageView mIv_icon;
    private TextView mTv_note;

    //设置图标
    public void setIv_icon(int resId) {
        mIv_icon.setImageResource(resId);
    }
    //设置文本
    public void setTv_note(String note) {
        mTv_note.setText(note);
    }



    private long mMax = 100;
    private long mProgress ;
    private boolean isPregressEnable = true;
    //设置成进度条类型
    public void setMax(long max) {
        mMax = max;
    }

    public void setProgress(long progress) {
        mProgress = progress;
        //有了新的进度，重新绘制
        invalidate();
    }

    public void setIsPregressEnable(boolean isPregressEnable) {
        this.isPregressEnable = isPregressEnable;
    }


    public ProgressView(Context context) {
        this(context, null);
    }

    public ProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //挂载定义的布局
        //
        // 这里给this，就是自己为rootView.挂载布局到自身
        View view = ProgressView.inflate(context, R.layout.inflate_progress_view, this);

        //找到孩子对象
        mIv_icon = (ImageView) view.findViewById(R.id.progressview_iv_icon);
        mTv_note = (TextView) view.findViewById(R.id.progressview_tv_note);
    }

    //为了在下载标志周围画一个圈，显示进度
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);//绘制背景

    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);//绘制图标和文字
        if(isPregressEnable){
            //绘制圆弧
            RectF oval = new RectF(mIv_icon.getLeft(), mIv_icon.getTop(), mIv_icon.getRight(), mIv_icon.getBottom());
            float startAngle = -90;//起始的角度
            float sweepAngle = mProgress*1.0f / mMax*360;//扫过的角度
            boolean useCenter = false;//有没有半径的显示
            Paint paint = new Paint();
            paint.setColor(Color.BLUE);
            paint.setStyle(Paint.Style.STROKE);//STROKE画宽，FILL填充
            paint.setStrokeWidth(3);//宽的像素
            //用画笔要消除锯齿
            paint.setAntiAlias(true);

            canvas.drawArc(oval, startAngle, sweepAngle, useCenter, paint);
        }
    }
}
